home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / 13HLIB.ZIP / SPRDEMO.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-17  |  4.3 KB  |  150 lines

  1. /*
  2. Sprdemo.cpp : Demo of Sprite capabilities in Mode13hLib.
  3. */
  4.  
  5. //Includes
  6. #include "13hlib.h"
  7. #include <stdio.h>
  8. #include <dos.h>
  9. #include <stdlib.h>
  10.  
  11. //Defines
  12. #define WIDTH 48
  13. #define HEIGHT 48
  14.  
  15. //Function declarations
  16. void GrabSprites();
  17. void MoonWalk();
  18. void KillSprites();
  19. void CloseDown(char *message);
  20.  
  21. //Globals
  22. Mode13hLib Mode13h;
  23. Sprite Spaceman[4];
  24.  
  25. void main()
  26. {
  27.  if(Mode13h.DetectVGA() == FAILURE) CloseDown("You need a VGA card to run this program.");
  28.  Mode13h.SetMode13h();
  29.  if(Mode13h.SetUpPage() == FAILURE) CloseDown("Not enough memory to allocate virtual screen page.");
  30.  Mode13h.ClearScreen(0);
  31.  //Mode13h.LoadPCXFile("spaceman.pcx");
  32.  GrabSprites();
  33.  MoonWalk();
  34.  KillSprites();
  35.  Mode13h.ClosePage();
  36.  Mode13h.CloseMode13h();
  37. }
  38.  
  39. void GrabSprites()
  40. {
  41.  Mode13h.LoadPalette("space.pal");
  42.  Spaceman[1] = Mode13h.ReadSpriteFromDisk("space1.m13");
  43.  Spaceman[2] = Mode13h.ReadSpriteFromDisk("space2.m13");
  44.  Spaceman[3] = Mode13h.ReadSpriteFromDisk("space3.m13");
  45.  
  46.  //Error checking code.
  47.  if((Spaceman[1] == NULL) || (Spaceman[2] == NULL) || (Spaceman[3] == NULL))
  48.    CloseDown("Can't load Sprites.\nCheck for the files space1.m13, space2.m13 and space3.m13.\n");
  49.  /*
  50.  Spaceman[1] = Mode13h.GetSprite(192, 0, WIDTH, HEIGHT);
  51.  Spaceman[2] = Mode13h.GetSprite(240, 0, WIDTH, HEIGHT);
  52.  Spaceman[3] = Mode13h.GetSprite(0, 48, WIDTH, HEIGHT);
  53.  
  54.  Mode13h.WriteSpriteToDisk("space1.m13", WIDTH, HEIGHT, Spaceman[1]);
  55.  Mode13h.WriteSpriteToDisk("space2.m13", WIDTH, HEIGHT, Spaceman[2]);
  56.  Mode13h.WriteSpriteToDisk("space3.m13", WIDTH, HEIGHT, Spaceman[3]);
  57.  */
  58. }
  59.  
  60. void KillSprites()
  61. {
  62.  KillSprite(Spaceman[1]);
  63.  KillSprite(Spaceman[2]);
  64.  KillSprite(Spaceman[3]);
  65. }
  66.  
  67. void MoonWalk()
  68. {
  69.  Sprite BackSpr;
  70.  int currtextx = 15;
  71.  int currmod = 1;
  72.  int spacex = 250, spacey = 150;
  73.  int curr_sprite = 2;
  74.  int timedel;
  75.  int currsprdelay = 2;       //Current Sprite delay value.
  76.  struct time time1;
  77.  struct time time2;     //Used for video card profiling
  78.  randomize();
  79.  gettime(&time1);
  80.  for(int j = 0; j<=175; j++)  Mode13h.PlotPixel(random(320), random(200), 239);
  81.  
  82.  //Start title bar
  83.  Mode13h.Bar(1, 1, 318, 20, 7);
  84.  Mode13h.Rectangle(1, 1, 318, 20, 15);
  85.  Mode13h.HorizontalLine(1, 21, 318, 8);
  86.  Mode13h.VerticalLine(319, 2, 19, 8);
  87.  Mode13h.BltText("-- Sprdemo by Pri$m --", 16, 8, 16);
  88.  Mode13h.BltText("-- Sprdemo by Pri$m --", 15, 7, 31);
  89.  Mode13h.PlotPixel(319, 1, 7);
  90.  Mode13h.PlotPixel(1, 21, 7);
  91.  BackSpr = Mode13h.GetSprite(spacex, spacey, WIDTH, HEIGHT);
  92.  Mode13h.DrawSprite(spacex, spacey, WIDTH, HEIGHT, Spaceman[1]);
  93.  gettime(&time2);
  94.  
  95.  //Profiling calcs
  96.  if(time1.ti_hund > time2.ti_hund) timedel = 10 * (100 - time1.ti_hund + time2.ti_hund);
  97.   else timedel = 10 * (time1.ti_hund - time2.ti_hund);
  98.  //That's why we had the 10x loop - we now have a fairly accurate
  99.  //account of how fast it would do the loop in milliseconds.
  100.  //We want around 30fps. So ... we minus this value from 33 millisecs. (1000 / 30 = 33)
  101.  timedel = 33 - timedel;
  102.  //Profiling complete! Now it will run at the same speed on all video cards.
  103.  //Each time it calculates it's own delay.
  104.  //This is useful for games - but then it should be stored in some sort of
  105.  //configuration file, so that it's only done once.
  106.  
  107.  for(int k =0; k <=500; k++)
  108.  {
  109.   currtextx += currmod;
  110.   if(currtextx == 143 || currtextx == 5)
  111.    currmod = currmod * -1;
  112.   else
  113.    {
  114.     Mode13h.Bar(currtextx-1, 6, 177, 14, 7);
  115.     Mode13h.BltText("-- Sprdemo by Pri$m --", currtextx + 1, 8, 16);
  116.     Mode13h.BltText("-- Sprdemo by Pri$m --", currtextx, 7, 31);
  117.    }
  118.  if(currsprdelay == 0)
  119.  {
  120.  Mode13h.DrawSpriteNoTrans(spacex, spacey, WIDTH, HEIGHT, BackSpr);
  121.   if(spacex != 0) spacex -= 10;
  122.    else{
  123.     spacex = 270;
  124.     spacey -= 20;
  125.     }
  126.   KillSprite(BackSpr);
  127.   BackSpr = Mode13h.GetSprite(spacex, spacey, WIDTH, HEIGHT);
  128.   Mode13h.DrawSprite(spacex, spacey, WIDTH, HEIGHT, Spaceman[curr_sprite]);
  129.    if(curr_sprite == 1)
  130.      curr_sprite = 3;
  131.    else curr_sprite -= 1;
  132.    currsprdelay = 2;
  133.   }
  134.    else currsprdelay -= 1;
  135.    delay(timedel);
  136.    Mode13h.WaitVerticalRetrace();
  137.    Mode13h.CopyPageToScreen();
  138.  }
  139. }
  140.  
  141. void CloseDown(char *message)
  142. {
  143.  Mode13h.CloseMode13h();
  144.  Mode13h.ClosePage();
  145.  delete Spaceman[1];
  146.  delete Spaceman[2];
  147.  delete Spaceman[3];
  148.  puts(message);
  149.  abort();
  150. }